<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="dataform" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input v-model="form.nickName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="登录账号" prop="phonenumber">
            <el-input  v-model="form.userName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">角色信息</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)">
      <el-table-column label="序号" type="index" align="center">
        <template #scope>
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
      <el-table-column label="角色编号" align="center" prop="roleId" />
      <el-table-column label="角色名称" align="center" prop="roleName" />
      <el-table-column label="权限字符" align="center" prop="roleKey" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #scope>
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      v-show="total>0"
      :total="total"
      background
      layout="total, prev, pager, next"
      v-model:current-page="pageNum"
      :limit="pageSize"
      @current-change="getList"
    />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts">
import { getAuthRole, updateAuthRole } from "@/apis/system/user";
import { defineComponent, getCurrentInstance, onMounted, reactive, toRefs, nextTick } from "@vue/runtime-core";
export default defineComponent({
  setup(){
    const { proxy }=getCurrentInstance();
    const dataMap=reactive({
      // 遮罩层
      loading: true,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 选中角色编号
      roleIds:[],
      // 角色信息
      roles: [],
      // 用户信息
      form: {},
      /** 单击选中行数据 */
      clickRow(row) {
        (proxy as any).$refs.table.toggleRowSelection(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        dataMap.roleIds = selection.map((item) => item.roleId);
      },
      // 保存选中的数据编号
      getRowKey(row) {
        return row.roleId;
      },
      /** 提交按钮 */
      submitForm() {
        const userId = dataMap.form['userId'];
        const roleIds = dataMap.roleIds.join(",");
        updateAuthRole({ userId: userId, roleIds: roleIds }).then((response) => {
          (proxy as any).msgSuccess("授权成功");
          this.close();
        });
      },
      /** 关闭按钮 */
      close() {
        (proxy as any).$store.dispatch("tagsView/delView", this.$route);
        (proxy as any).$router.push({ path: "/system/user" });
      },
    })
    onMounted(()=>{
      const userId = (proxy as any).$route.params && (proxy as any).$route.params.userId;
      if (userId) {
        dataMap.loading = true;
        getAuthRole(userId).then((response) => {
          dataMap.form = response.user;
          dataMap.roles = response.roles;
          dataMap.total = dataMap.roles.length;
          nextTick(() => {
            dataMap.roles.forEach((row) => {
              if (row.flag) {
                (proxy as any).$refs.table.toggleRowSelection(row);
              }
            });
          });
          dataMap.loading = false;
        });
      }
    })
    return{
      ...toRefs(dataMap)
    }
  }
})
</script>